package xnj.lazydog.btcontroller.Bluetooth;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class DeviceConnectView extends View {

    Paint p;
    Resources res;
    BlueToothMaster master;
    BluetoothDevice device;

    float width,height;
    int STATE_CONNECTING = 1;
    int STATE_IDLE  =0;
    int STATE_CONNECTED = 2;
    int state=STATE_IDLE;

    public DeviceConnectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        p = new Paint();
        res = getResources();
        p.setAntiAlias(true);
    }

    float edge_ratio = 0.1f;
    float out_circle_ratio = 0.45f;
    float inner_cross_ratio = 0.2f;
    float inner_cross_w_ratio = 0.05f;
    float inner_cross_c_ratio = 0.28f;

    float deg_scale = (inner_cross_c_ratio-inner_cross_ratio)/(inner_cross_ratio*45);

    public void start()
    {
        state = STATE_CONNECTING;
    }
    public void finished()
    {
        state = STATE_CONNECTED;
    }
    public void stop()
    {
        state = STATE_IDLE;
    }

    float ph=0;
    float scl=1;
    float deg=0;

    float k = 0.1f;
    float center_w,center_h;

    float d_scl;


    int IDLE_COLOR = 0xff80aaee;
    int CTD_COLOR = 0xffee6835;

    int getMiddleColor(int c1,int c2,float p)
    {
        float a,r,g,b;

        a = Color.alpha(c1)+(Color.alpha(c2)-Color.alpha(c1)*1.0f)*p;
        r = Color.red(c1)+(Color.red(c2)-Color.red(c1)*1.0f)*p;
        g = Color.green(c1)+(Color.green(c2)-Color.green(c1)*1.0f)*p;
        b = Color.blue(c1)+(Color.blue(c2)-Color.blue(c1)*1.0f)*p;

        if(a>255)
            a=255;
        if(a<0)
            a = 0;

        if(r<0)
            r = 0;
        if(g<0)
            g = 0;
        if(b<0)
            b = 0;
        if(r>255)
            r = 255;
        if(g>255)
            g = 255;
        if(b>255)
            b = 255;

        return Color.argb((int)a,(int)r,(int)g,(int)b);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        width = getMeasuredWidth();
        height = getMeasuredHeight();
        center_w = width*0.5f;
        center_h = height*0.5f;

        float px = width<height?width:height;

       // canvas.drawARGB(255,55,88,123);

        state = STATE_IDLE;
        if(device!=null) {
            if (master.deviceConnected != null ) {
                if (master.deviceConnected.getAddress().equals(device.getAddress())&&master.state==BlueToothMaster.STATE_CONNECTED) {
                    state = STATE_CONNECTED;
                }
            }
            if(master.deviceToConnect!=null)
            {
                if(master.deviceToConnect.getAddress().equals(device.getAddress())&&master.state==BlueToothMaster.STATE_CONNECTING)
                {
                 state = STATE_CONNECTING;
                }
            }
        }


        if(state==STATE_CONNECTING)
        {
            ph+=0.12f;
            scl +=  (1+Math.sin(ph)*0.4f-scl)*k;
            deg +=-deg*k;
        }else if(state==STATE_CONNECTED)
        {
            ph = 0;
            scl +=(1-scl)*k;
            deg+=(45-deg)*k;
        }else
        {
            ph = 0;
            scl +=(1-scl)*k;
            deg+=(-deg)*k;
        }


        p.setARGB(255,255,255,255);
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(width*edge_ratio);
        canvas.drawCircle(center_w,center_h,px*out_circle_ratio,p);
        p.setColor(getMiddleColor(IDLE_COLOR,CTD_COLOR,deg/45));
        p.setStyle(Paint.Style.FILL);
        canvas.save();
        canvas.translate(center_w,center_h);
        canvas.rotate(deg);

        d_scl = 1*scl+deg*deg_scale;

        canvas.scale(d_scl,d_scl);

        canvas.drawRoundRect(-px*inner_cross_ratio,-px*inner_cross_w_ratio,px*inner_cross_ratio,px*inner_cross_w_ratio,px*inner_cross_w_ratio,px*inner_cross_w_ratio,p);
        canvas.drawRoundRect(-px*inner_cross_w_ratio,-px*inner_cross_ratio,px*inner_cross_w_ratio,px*inner_cross_ratio,px*inner_cross_w_ratio,px*inner_cross_w_ratio,p);

        canvas.restore();

        invalidate();
    }
}
